package com.example.home.view;

import androidx.core.content.ContextCompat;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.common.gamedetail.GameDetailActivity;
import com.example.home.IStartGameDetailActivity;
import com.example.home.PreonlineRAdapter;
import com.example.home.R;
import com.example.common.component.RecyclerOnScrollListener;
import com.example.home.databinding.HomePreonlineFragmentBinding;
import com.example.home.viewmodel.HomePreonlineViewModel;
import com.example.home.viewmodel.HomeRecommendViewModel;
import com.example.home.viewmodel.HomeViewModel;

import org.jetbrains.annotations.NotNull;

import java.text.ParseException;

public class HomePreonlineFragment extends Fragment {

    private static final String TAG = "HomePreonlineFragment";
    private HomePreonlineViewModel viewModel;
    private RecyclerView recyclerView;
    private SwipeRefreshLayout swipeRefreshLayout;
    private HomePreonlineFragmentBinding binding;

    public static HomePreonlineFragment newInstance() {
        return new HomePreonlineFragment();
    }

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        binding= DataBindingUtil.inflate(inflater,
                R.layout.home_preonline_fragment,container,false);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView = view.findViewById(R.id.home_preonline_recyclerview);
        swipeRefreshLayout = view.findViewById(R.id.home_preonline_swiperefresh);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        viewModel = new ViewModelProvider(requireActivity()).get(HomePreonlineViewModel.class);
        // TODO: Use the ViewModel
        binding.setData(viewModel);
        binding.setLifecycleOwner(requireActivity());

        //这里是recyclerView的设置
        PreonlineRAdapter adapter = new PreonlineRAdapter(viewModel);
        recyclerView.setLayoutManager(new LinearLayoutManager(requireActivity()));
        recyclerView.setAdapter(adapter);

        //adapter的item绑定点击事件
        adapter.iStartGameDetailActivity=new IStartGameDetailActivity() {
            @Override
            public void openById(Integer id) {
                Intent intent=new Intent(requireActivity(), GameDetailActivity.class);
                intent.putExtra("ID",id);
                startActivity(intent);
            }
        };

        //下拉刷新设置颜色
        swipeRefreshLayout.setColorSchemeColors(ContextCompat.getColor(requireActivity(), R.color.theme_color));
        //下拉刷新设置监听
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                viewModel.loadTop();//数据加载
            }
        });

        //监听上拉下拉加载
        viewModel.getLTorLD().observe(requireActivity(), new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                if(aBoolean){
                    adapter.notifyItemRangeInserted(0, HomePreonlineViewModel.LOAD_NUMBER);
                    swipeRefreshLayout.setRefreshing(false);
                    //加载完成后移动到某个位置
                    recyclerView.smoothScrollToPosition(0);
                }else {
                    adapter.notifyItemRangeInserted(adapter.getItemCount()
                            , HomePreonlineViewModel.LOAD_NUMBER);
                }
            }
        });

        //上拉刷新
        recyclerView.addOnScrollListener(new RecyclerOnScrollListener() {
            @Override
            public void loadMore() {
                viewModel.loadDown();//数据更新
            }

            @Override
            public void showTopData(int position) throws ParseException {
                viewModel.setTopDate(position);
            }
        });

        if(viewModel.isFirstLoad){
            //刚进页面时显示加载条
            swipeRefreshLayout.setRefreshing(true);
            viewModel.loadTop();
            viewModel.isFirstLoad=false;
        }
    }

    @Override
    public void onPause() {
        super.onPause();//切换页面时取消加载
        swipeRefreshLayout.setRefreshing(false);
    }
}